package com.ruoyi.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.OvertimeApplicationsMapper;
import com.ruoyi.system.domain.OvertimeApplications;
import com.ruoyi.system.service.IOvertimeApplicationsService;

/**
 * 加班Service业务层处理
 */
@Service
public class OvertimeApplicationsServiceImpl implements IOvertimeApplicationsService
{
    @Autowired
    private OvertimeApplicationsMapper overtimeApplicationsMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public String getUserNameById(Long userId) {
        SysUser user = sysUserMapper.selectUserById(userId);
        return user != null ? user.getUserName() : String.valueOf(userId);
    }

    @Override
    public Map<String, Object> getMonthOvertimeStats(Long userId, Integer month, Integer year) {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("month", month);
        params.put("year", year);

        Map<String, Object> result = new HashMap<>();
        // 获取本月加班天数
        Integer days = overtimeApplicationsMapper.selectMonthOvertimeDays(params);
        // 获取本月加班总时长
        Double hours = overtimeApplicationsMapper.selectMonthOvertimeHours(params);

        result.put("days", days != null ? days : 0);
        result.put("hours", hours != null ? hours : 0.0);
        return result;
    }

    @Override
    public OvertimeApplications selectOvertimeApplicationsById(Long id) {
        return overtimeApplicationsMapper.selectOvertimeApplicationsById(id);
    }

    @Override
    public List<OvertimeApplications> selectOvertimeApplicationsList(OvertimeApplications overtimeApplications) {
        return overtimeApplicationsMapper.selectOvertimeApplicationsList(overtimeApplications);
    }

    @Override
    public int insertOvertimeApplications(OvertimeApplications overtimeApplications) {
        return overtimeApplicationsMapper.insertOvertimeApplications(overtimeApplications);
    }

    @Override
    public int updateOvertimeApplications(OvertimeApplications overtimeApplications) {
        return overtimeApplicationsMapper.updateOvertimeApplications(overtimeApplications);
    }

    @Override
    public int deleteOvertimeApplicationsByIds(Long[] ids) {
        return overtimeApplicationsMapper.deleteOvertimeApplicationsByIds(ids);
    }

    @Override
    public int deleteOvertimeApplicationsById(Long id) {
        return overtimeApplicationsMapper.deleteOvertimeApplicationsById(id);
    }

    @Override
    public List<SysUser> selectDeptManagersByUserId(Long userId) {
        // 修改：明确传入角色ID 102（经理角色）
        return overtimeApplicationsMapper.selectDeptManagersByUserId(userId, 102L);
    }

    @Override
    public List<OvertimeApplications> selectOvertimeApplicationsListByRole(OvertimeApplications overtimeApplications, Long currentUserId) {
        return overtimeApplicationsMapper.selectOvertimeApplicationsListByRole(overtimeApplications, currentUserId);
    }
}